package com.china08.yxyapi.entity.db.questionbank;

import java.io.IOException;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.log4j.Logger;

import com.china08.yxyapi.entity.BaseEntityNoId;
import com.china08.yxyapi.entity.mg.res.AllTemplate;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

/**
 * 
 * 
 * 题库: 试题信息</br>
 * ## 本实体类存在的原因是因为在进行foundation题库(旧题库) 和新题库进行合并时,兼容已经上线的产品冗余字段.</br>
 * ## 试题查询可以不显示本表中的字段(因为本表中的字段和 yxy_quesbank_questions 表中的值相同)</br>
 * ##试题维护时,进行保存,如果试题为foundation的旧题,需要在yxy_quesbank_questions和yxy_quesbank_questions_foundation表中都进行修改存储</br>
 * ## 2018年01月05日 Richard.Wang
 *
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "yxy_quesbank_questions_foundation", catalog = "yxy_question_bank")
public class YxyQuesbankQuestionsFoundation extends BaseEntityNoId {

	@Transient
	private static final Logger LOGGER = Logger.getLogger(YxyQuesbankQuestionsFoundation.class);

	/**
	 * 试题id
	 */
	@Id
	@Column(length = 32)
	private String id;

	// some properties
	@Column(name = "difficulty_level", columnDefinition = "char(1)")
	private String difficultyLevel;

	@Column(name = "difficulty_xs")
	private Float difficultyXishu;

	@Column(name = "ability_level", columnDefinition = "char(1)")
	private String abilityLevel;

	@Column(name = "knowledge_ids")
	private String knowledgeIds;

	@Column(name = "know_full_ids")
	private String knowFullIds;

	@Column(name = "knowledge_texts", columnDefinition = "text")
	private String knowledgeTexts;

	@Column(name = "src_type", columnDefinition = "char(3)")
	private String srcType;

	@Column(name = "src_name", columnDefinition = "varchar(100)")
	private String srcName;

	@Column(name = "question_type", columnDefinition = "char(3)")
	private String questionType;

	@Column(name = "question_score", columnDefinition = "varchar(20)")
	private String questionScore;

	@Column(name = "body", columnDefinition = "text")
	private String body;

	@Transient
	private Object quesBody;

	public Object getQuesBody() throws JsonParseException, JsonMappingException, IOException {
		try {
			JacksonXmlModule module = new JacksonXmlModule();
			module.setDefaultUseWrapper(true);
			XmlMapper xmlMapper = new XmlMapper(module);
			this.body = this.body.replace(",yxup", "http://yx-tiku-img.oss-cn-beijing.aliyuncs.com/yxup");
			this.body = this.body.replace("img.xueshentong.com", "yx-tiku-img.oss-cn-beijing.aliyuncs.com");
			this.body = this.body.replace("src=\"\\quiz", "src=\"http://yx-tiku-img.oss-cn-beijing.aliyuncs.com\\quiz");

			this.quesBody = xmlMapper.readValue(this.body, AllTemplate.class);
		} catch (Exception e) {
			LOGGER.error("ques(" + this.id + ")-->quesBody 转换（template）异常!(tyw)", e);
		}
		return this.quesBody;
	}

}
